// pages/consumeRecord/consumeRecord.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

    conditions1: {
      start_time: '',
      end_time: '',
    },

    conditions2: {
      start_time: '',
      end_time: '',
    },

    drawer: false,
    isFilter: false,

    rechargeList: [],
    page0: 1,
    page_count0: 1,
    noMore0: false,
    loading0: false,
    costList: [],
    page: 1,
    page_count: 1,
    noMore: false,
    loading: false,
    row: 10,
    id: '',
    sn: '',
    currentTab: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //console.log(options.id)
    that.setData({
      id: options.id,
      sn: options.sn
    })
    that.getRechargeRecord();
    that.getcostRecord();
  },



  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 获取充值记录
  getRechargeRecord: function () {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/card_recharge_record',
      data: {
        water_id: that.data.id,
        card: that.data.sn,
        page: that.data.page0,
        row: that.data.row,
        ...that.data.conditions1
      },
      success: function (res) {
        var rechargeList = that.data.rechargeList.concat(res.data.data.data);
        var len = res.data.data.data.length;
        if (res.data.code == 1000) {
          if (that.data.row > len) {
            that.setData({
              noMore0: true
            })
          } else {
            that.setData({
              noMore0: false
            })
          }
          that.setData({
            rechargeList: rechargeList,
            page_count0: res.data.data.page.page_count,
            drawer:false
          })
        }
      },
    })
  },

  // 获取消费记录
  getcostRecord: function () {
    var that = this;
    app.ajax({
      url: 'Partner/WaterCard/waterPurchaseHistory',
      data: {
        id: that.data.id,
        page: that.data.page,
        row: that.data.row,
        ...that.data.conditions2
      },
      success: function (res) {
        console.log(res)
        var costList = that.data.costList.concat(res.data.data.data);
        var len = res.data.data.data.length;
        if (res.data.code == 1000) {
          if (that.data.row > len) {
            that.setData({
              noMore: true
            })
          } else {
            that.setData({
              noMore: false
            })
          }
          that.setData({
            costList: costList,
            page_count: res.data.data.page.page_count,
            drawer:false
          })
        }
      },
    })
  },

  // 上拉加载
  onReachBottom: function () {
    var that = this;
    if (that.data.currentTab == 0) {
      // 充值记录
      var page = that.data.page0 + 1;
      if (page > that.data.page_count0) {
        that.setData({
          noMore0: true
        })
        return false;
      }
      that.setData({
        page0: page
      })
      that.getRechargeRecord();
    } else if (that.data.currentTab == 1) {
      // 消费记录
      var page = that.data.page + 1;
      if (page > that.data.page_count) {
        that.setData({
          noMore: true
        })
        return false;
      }
      that.setData({
        page: page
      })
      that.getcostRecord();
    }
  },

  // 切换选项卡
  switchtab: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
  },



  showDrawer() {
    this.condition = {
      ...this.data.currentTab == 0 ? this.data.conditions1 : this.data.conditions2
    };
    this.setData({
      drawer: true,
    })
  },

  closeDrawer() {
    this.setData({
      drawer: false,
      [`${this.data.currentTab ==0?'conditions1':'conditions2'}`]: this.condition
    })
    delete(this.condition)
  },

  resetDrawer() {
    const conditions = this.data.currentTab == 0 ? this.data.conditions1 : this.data.conditions2;
    for (let x in conditions) {
      conditions[x] = "";
    }
    this.setData({
      [`${this.data.currentTab ==0 ?'conditions1':'conditions2'}`]: conditions,
    })
  },

  confirmDrawer() {
    let isFilter = false;
    const conditions = this.data.currentTab == 0 ? this.data.conditions1 : this.data.conditions2;
    for (let x in conditions) {
      conditions[x] !== "" && (isFilter = true);
    }
    this.setData({
      isFilter
    })
    delete(this.condition);

    if(this.data.currentTab==0){
      this.setData({
        rechargeList: [],
        page0:1,
      })
  
      this.getRechargeRecord();
    }else{
      this.setData({
        costList: [],
        page:1,
      })
  
      this.getcostRecord();
    }
  },



  handleCondition(e) {
    const name = e.currentTarget.dataset.name;
    let pname = this.data.currentTab == 0 ? 'conditions1' : 'conditions2'
    this.setData({
      [`${pname}.${name}`]: e.detail.value
    })
  },


})